package org.spring.qq_chat_java.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.spring.qq_chat_java.constant.ReceiverType;
import org.spring.qq_chat_java.pojo.*;
import org.spring.qq_chat_java.pojo.uto.chatListUTO;
import org.spring.qq_chat_java.service.GroupMembersService;
import org.spring.qq_chat_java.service.chatGroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/groups")
@CrossOrigin(origins = "*")
@RequiredArgsConstructor
public class GroupController {
    private final chatGroupService chatGroupService;
    private final GroupMembersService groupMembersService;

    //获得用户id加入的群
    @GetMapping
    public Result<List<chatListUTO>> getUserGroups(@RequestParam  Long id) {
        //根据userId查询GroupMembers
        List<GroupMembers>  groupMembers = groupMembersService.list(new QueryWrapper<GroupMembers>().eq("user_id",id));

        //根据groupId查询chatGroup
        List<chatListUTO> chatListUTO = groupMembers.stream().map(groupMembers1 -> {
            chatGroup chatGroup = chatGroupService.getById(groupMembers1.getGroupId());
            return  new chatListUTO(chatGroup.getGroupId(),chatGroup.getAvatar(), chatGroup.getName(), chatGroup.getDescription(), ReceiverType.group,chatGroup.getMaxMembers());
        }).toList();
        return Result.success(chatListUTO);

    }
}


















